package com.controller;

import com.dto.CommentDTO;
import com.dto.ReaderCommentDTO;
import com.service.CommentServiceImpl;
import com.service.LoginServiceImpl;
import com.util.LykFileUtil;
import com.util.Result;
import com.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author linyukun
 * @date 2022/10/18 15:56
 */
@RequestMapping("comment")
@RestController
public class CommentController {
    @Autowired
    private LoginServiceImpl loginServiceImpl;

    @Autowired
    private CommentServiceImpl commentServiceImpl;

    //审核中的数据
    private volatile Map<String,String> commentControllerMap = new HashMap<>();
    private static final String commentControllerMapSeparator = "*****";

    /**
     * 新增评论
     * @author linyukun
     * @date 2022-10-18 16:44:00
     */
    @RequestMapping(value="/addComment",method = RequestMethod.POST)
    public Result addComment(@RequestBody CommentDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setCreateUser(userName);
            return ResultUtil.returnDataSuccess(commentServiceImpl.addComment(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }

    /**
     * 删除评论
     * @author linyukun
     * @date 2022-10-18 16:44:00
     */
    @RequestMapping(value="/delComment",method = RequestMethod.POST)
    public Result delComment(@RequestBody CommentDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            dto.setDeleteUser(userName);
            return ResultUtil.returnDataSuccess(commentServiceImpl.delComment(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


    /**
     * 回复评论
     * @author linyukun
     * @date 2022-10-18 16:44:00
     */
//    @RequestMapping(value="/replyReaderReply",method = RequestMethod.POST)
//    public Result replyReaderReply(@RequestBody ReaderCommentDTO dto){
//        try{
//            String userName = loginServiceImpl.isLogin();
//            dto.setReplyUser(userName);
//            return ResultUtil.returnDataSuccess(readerCommentServiceImpl.replyReaderReply(dto));
//        }catch(Exception e){
//            e.printStackTrace();
//            return ResultUtil.returnFailMsg(e.getMessage());
//        }
//    }

    /**
     * 获取评论列表
     * @author linyukun
     * @date 2022-10-18 16:46:00
     */
    @RequestMapping(value="/getCommentList",method = RequestMethod.POST)
    public Result getCommentList(@RequestBody CommentDTO dto){
        try{
            String userName = loginServiceImpl.isLogin();
            if(!"admin".equals(dto.getIdentity())){
                dto.setCreateUser(userName);
            }
            return ResultUtil.returnDataSuccess(commentServiceImpl.getCommentList(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }
    }


    /**
     * 审核评论
     * @author linyukun
     * @date 2022-10-18 16:46:00
     */
    @RequestMapping(value="/auditComment",method = RequestMethod.POST)
    public Result auditComment(@RequestBody CommentDTO dto){
        String judgeKey = "auditComment" + commentControllerMapSeparator + dto.getId();
        try{
            loginServiceImpl.judgeIsOnTimeRequest(commentControllerMap, judgeKey);
            String userName = loginServiceImpl.isLogin();
            dto.setAuditUser(userName);
            return ResultUtil.returnDataSuccess(commentServiceImpl.auditComment(dto));
        }catch(Exception e){
            e.printStackTrace();
            return ResultUtil.returnFailMsg(e.getMessage());
        }finally {
            commentControllerMap.remove(judgeKey);
        }
    }





}
